<template>
  <div class="app-container">
    <el-row>
      <!--字典类型管理开始-->
      <el-col :span="12">
        <el-card>
          <!--搜索表单开始-->
          <el-form :inline="true" size="small" :model="page.params">
            <el-form-item>
              <el-input v-model="page.params.dictName" placeholder="字典名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-select clearable v-model="page.params.status" placeholder="请选择状态">
                <el-option
                  v-for="item in statusOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="small" @click="search">查询</el-button>
              <el-button type="warning" icon="el-icon-refresh" size="small" @click="page.params={}">重置</el-button>
              <el-button type="success" icon="el-icon-plus" size="small" @click="toAddDictType">添加字典类型</el-button>
            </el-form-item>
          </el-form>
          <!--搜索表单结束-->
          <!--字典类型数据表格开始-->
          <div>
            <el-table
              v-loading="loading"
              :data="dataPage.list"
              border
              stripe
              @row-click="dictTypeRowClick"
              style="width: 100%"
            >
              <el-table-column prop="dictId" label="类型编号" align="center"></el-table-column>
              <el-table-column prop="dictName" label="类型名称" align="center"></el-table-column>
              <el-table-column prop="dictType" label="字典类型" align="center"></el-table-column>
              <el-table-column prop="remark" label="类型备注" align="center"></el-table-column>
              <el-table-column prop="status" label="状态" align="center">
                <template slot-scope="{row}">
                  <dict-tag :data-list="statusOptions" :value="row.status"></dict-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="{row}">
                  <el-dropdown>
                    <span class="el-dropdown-link">
                      <el-link icon="el-icon-s-operation" type="primary">操作<i
                        class="el-icon-arrow-down el-icon--right"></i></el-link>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item>
                        <el-button type="text" icon="el-icon-edit" @click="toUpdateDictType(row)">修改</el-button>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <el-button type="text" icon="el-icon-delete" @click="toDeleteDictType(row)">删除</el-button>
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page.pageNumber"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="page.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="dataPage.totalCount">
            </el-pagination>
          </div>
          <!--字典类型数据表格结束-->
        </el-card>
      </el-col>
      <!--字典类型管理结束-->

      <!--字典数据管理开始-->
      <el-col :span="12">
        <el-card>
          <el-form>
            <el-form-item>
              <el-button type="success" icon="el-icon-plus" size="small" :disabled="currentDictType==undefined"
                         @click="toAddDictData">添加字典数据
              </el-button>
              <el-button type="success" icon="el-icon-refresh" size="small" @click="toRefreshCache">同步缓存</el-button>
            </el-form-item>
          </el-form>
          <el-table
            v-loading="dataLoading"
            :data="dictDataList"
            border
            stripe
            style="width: 100%"
          >
            <el-table-column prop="dictCode" label="字典编码" align="center"></el-table-column>
            <el-table-column prop="dictLabel" label="字典标签" align="center"></el-table-column>
            <el-table-column prop="dictValue" label="字典键值" align="center"></el-table-column>
            <el-table-column prop="dictSort" label="字典排序" align="center"></el-table-column>
            <el-table-column prop="status" label="状态" align="center">
              <template slot-scope="{row}">
                <dict-tag :data-list="statusOptions" :value="row.status"></dict-tag>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" align="center"></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="{row}">
                <el-dropdown>
                    <span class="el-dropdown-link">
                      <el-link icon="el-icon-s-operation" type="primary">操作<i
                        class="el-icon-arrow-down el-icon--right"></i></el-link>
                    </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>
                      <el-button type="text" icon="el-icon-edit" @click="toUpdateDictData(row)">修改</el-button>
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-button type="text" icon="el-icon-delete" @click="toDeleteDictData(row)">删除</el-button>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>

        </el-card>
      </el-col>
      <!--字典数据管理结束-->
    </el-row>


    <!--字典类型添加的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="addDictTypeDialog"
               v-if="addDictTypeDialog">
      <sys-dict-type-add @after="getDictTypeByPage" @close="closeDialog"></sys-dict-type-add>
    </el-dialog>
    <!--字典类型添加的弹出层结束-->

    <!--字典类型修改的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="updateDictTypeDialog"
               v-if="updateDictTypeDialog">
      <sys-dict-type-update :active-id="dictTypeActiveId" @after="getDictTypeByPage"
                            @close="closeDialog"></sys-dict-type-update>
    </el-dialog>
    <!--字典类型修改的弹出层结束-->

    <!--字典数据添加的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="addDictDataDialog"
               v-if="addDictDataDialog">
      <sys-dict-data-add @after="queryDictDataByDictType" :active-type="currentDictType.dictType"
                         @close="closeDialog"></sys-dict-data-add>
    </el-dialog>
    <!--字典数据添加的弹出层结束-->

    <!--字典数据修改的弹出层开始-->
    <el-dialog :title="title"
               center
               width="50%"
               :visible.sync="updateDictDataDialog"
               v-if="updateDictDataDialog">
      <sys-dict-data-update @after="queryDictDataByDictType" :active-id="dictDataActiveId"
                            @close="closeDialog"></sys-dict-data-update>
    </el-dialog>
    <!--字典数据修改的弹出层结束-->
  </div>
</template>
<script>
import sysDictTypeApi from "@/api/system/sysDictType"
import sysDictDataApi from "@/api/system/sysDictData"
import sysDictTypeAdd from "./sys-dict-type-add"
import sysDictTypeUpdate from "./sys-dict-type-update"
import sysDictDataAdd from "./sys-dict-data-add"
import sysDictDataUpdate from "./sys-dict-data-update"

export default {
  name: "sys-dict-type-list",
  components: {
    sysDictTypeAdd,
    sysDictTypeUpdate,
    sysDictDataAdd,
    sysDictDataUpdate
  },
  data() {
    return {
      //字典类型加载中的进度条
      loading: false,
      //字典数据加载中的进度条
      dataLoading: false,
      //查询条件
      page: {
        pageNumber: 1,
        pageSize: 10,
        params: {
          dictName: undefined,
          status: undefined
        }
      },
      //字典类型的表格数据
      dataPage: {
        list: [],
        totalCount: 0
      },
      //字典数据的数组
      dictDataList: [],
      //弹出层的标题
      title: "",
      //是否打开添加字典类型的弹出层
      addDictTypeDialog: false,
      //是否打开修改字典类型的弹出层
      updateDictTypeDialog: false,
      //是否打开添加字典数据的弹出层
      addDictDataDialog: false,
      //是否打开修改字典数据的弹出层
      updateDictDataDialog: false,
      //当前选中的activeId
      dictTypeActiveId: undefined,
      //记录当前左边点击的字典类型
      currentDictType: undefined,
      //当前选中字典数据的activeId
      dictDataActiveId: undefined,
      //数据状态的字典数据
      statusOptions: []
    }
  },
  created() {
    this.getDictTypeByPage();
    this.getDataByType("sys_status_type").then(res => {
      this.statusOptions = res.data;
    })
  },
  methods: {
    //分页查询字典类型
    getDictTypeByPage() {
      this.loading = true;
      sysDictTypeApi.getByPage(this.page).then(res => {
        this.dataPage.list = res.data.list;
        this.dataPage.totalCount = res.data.totalCount
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    //每页条数发生变化时触发
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getDictTypeByPage()
    },
    //页码发生变化时触发
    handleCurrentChange(val) {
      this.page.pageNumber = val;
      this.getDictTypeByPage()
    },
    //字典类型行的点击事件
    dictTypeRowClick(row) {
      this.currentDictType = row;
      this.dataLoading = true
      sysDictDataApi.getDictDataByDictType(row.dictType).then(res => {
        this.dictDataList = res.data;
        this.dataLoading = false;
      }).catch(() => {
        this.dataLoading = false
      })
    },
    //搜索
    search() {
      this.getDictTypeByPage();
      this.dictDataList = []
    },
    //打开添加字典类型的弹出层
    toAddDictType() {
      this.addDictTypeDialog = true;
      this.title = "添加字典类型"
    },
    //打开修改字典类型的弹出层
    toUpdateDictType(row) {
      this.updateDictTypeDialog = true;
      this.title = "修改【" + row.dictName + "】字典类型"
      this.dictTypeActiveId = row.dictId;
    },
    //删除字典类型
    toDeleteDictType(row) {
      this.$confirm(`此操作会删除【${row.dictName}】该字典类型，它的字典数据也会全部删除, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sysDictTypeApi.deleteById(row.dictId).then(res => {
          this.$message.success('删除成功!');
          this.getDictTypeByPage();
        })
      }).catch(() => {
        this.$message.info('已取消删除!');
      });
    },
    //打开添加字典数据的弹出层
    toAddDictData() {
      this.title = `给【${this.currentDictType.dictName}】添加字典数据`
      this.addDictDataDialog = true
    },
    //打开修改字典数据的弹出层
    toUpdateDictData(row) {
      this.updateDictDataDialog = true;
      this.title = "修改【" + row.dictLabel + "】字典数据"
      this.dictDataActiveId = row.dictCode;
    },
    //删除字典数据
    toDeleteDictData(row) {
      this.$confirm(`此操作会删除【${row.dictLabel}】该字典数据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sysDictDataApi.deleteById(row.dictCode).then(res => {
          this.$message.success('删除成功!');
          this.queryDictDataByDictType();
        })
      }).catch(() => {
        this.$message.info('已取消删除!');
      });
    },
    //关闭所有弹出层
    closeDialog() {
      this.addDictTypeDialog = false;
      this.updateDictTypeDialog = false;
      this.addDictDataDialog = false;
      this.updateDictDataDialog = false;
    },
    //刷新右边的数据
    queryDictDataByDictType() {
      sysDictDataApi.getDictDataByDictType(this.currentDictType.dictType).then(res => {
        this.dictDataList = res.data;
        this.dataLoading = false;
      }).catch(() => {
        this.dataLoading = false
      })
    },
    //同步缓存数据
    toRefreshCache(){
      this.loading=true;
      sysDictTypeApi.refreshCache().then(res=>{
        this.$message.success(res.msg)
        this.loading=false
        this.dictDataList=[]
      }).catch(()=>{
        this.$message.error("同步失败")
        this.loading=false
      })
    }
  }
}
</script>

<style scoped>

</style>
